<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2019/7/2
 * Time: 0:41
 */

namespace app\api\controller\v1;


use app\common\lib\exception\ApiException;

class Comment extends AuthBase
{

    /**
     * 发表评论
     * @return array
     */
    public function save(){
        $data = input('post.', []);

        //news_id content to_news_id parent_id
        // validate 校验

        $data['user_id'] = $this->user->id;

        //判断文章是否存在
        //...

        try{
            $data['create_time'] = time();
            $commentId = model('Comment')->add($data);
            if ($commentId){
                model('News')->where(['id' => $data['news_id']])->setInc('comment_count');
                return show(config('code.success'), 'OK', [], 202);
            }else{
                return show(config('code.error'), '内部错误，评论失败', []. 500);
            }
        }catch (\Exception $e) {
            return show(config('code.error'), $e->getMessage(), [] . 500);
        }
    }

    /**
     * 评论列表 V 1.0
     * @return ApiException|array
     */
//    public function read(){
//        $newsId = input('param.id', 0, 'intval');
//        if (empty($newsId)){
//            return new ApiException('id is not', 404);
//        }
//
//        $param['news_id'] = $newsId;
//        $count = model('Comment')->getNormalCommentsCountByCondition($param);
//
//        $this->getPageAndSize(input('param.'));
//        $comments = model('Comment')->getNormalCommentsByCondition($param, $this->from, $this->size);
//
//        $result = [
//            'total' => $count,
//            'page_num' => ceil($count / $this->size),
//            'list' => $comments,
//        ];
//        return show(config('code.success'), 'OK', $result, 200);
//    }

    /**
     * 评论列表 V 2.0
     * @return ApiException|array
     */
    public function read(){
        $newsId = input('param.id', 0, 'intval');
        if (empty($newsId)){
            return new ApiException('id is not', 404);
        }

        $param['news_id'] = $newsId;
        $count = model('Comment')->getCountByCondition($param);

        $this->getPageAndSize(input('param.'));
        $comments = model('Comment')->getListsByCondition($param, $this->from, $this->size);

        if ($comments){
            foreach ($comments as $comment){
                $userIds[] = $comment['user_id'];
                if ($comment['to_user_id']){
                    $userIds[] = $comment['to_user_id'];
                }
            }
            $userIds = array_unique($userIds);
        }

        $userIds = model('User')->getUsersUserId($userIds);
        if (empty($userIds)){
            $userIdNames = [];
        }else{
            foreach ($userIds as $userId){
                $userIdNames[$userId->id] = $userId;
            }
        }

        $resultDatas = [];
        foreach ($comments as $comment){
            $resultDatas[] = [
                'id' => $comment->id,
                'user_id' => $comment->user_id,
                'to_user_id' => $comment->to_user_id,
                'content' => $comment->content,
                'username' => !empty($userIdNames[$comment->user_id]) ? $userIdNames[$comment->user_id]->username : '',
                'to_username' => !empty($userIdNames[$comment->to_user_id]) ? $userIdNames[$comment->to_user_id]->username : '',
                'parent_id' => $comment->parent_id,
                'create_time' => $comment->create_time,
                'image' => !empty($userIdNames[$comment->user_id]) ? $userIdNames[$comment->user_id]->image : '',
            ];
        }

        $result = [
            'total' => $count,
            'page_num' => ceil($count / $this->size),
            'list' => $resultDatas,
        ];
        return show(config('code.success'), 'OK', $result, 200);
    }


}